package cn.com.freelibra.forum.web;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.Page;

import cn.com.freelibra.forum.po.Keywords;
import cn.com.freelibra.forum.service.KeywordsService;
import cn.com.freelibra.forum.service.SectionService;
import org.springframework.ui.Model;

/**
 * 
 * @author qing
 *
 */
@Controller
@RequestMapping("/")
public class IndexController {
	@Resource
	private SectionService sectionService;
	
	@Resource
	private KeywordsService keywordsService;

	@RequestMapping("")
	public String root(Model model) {
		return "redirect:/index";
	}

	@RequestMapping("/index")
	public String index(Model model, String keyword, Integer pageNum, Integer pageSize) {
		Page<Map<String, Object>> result = sectionService.selectSectionJoinUserList(keyword, pageNum, pageSize);
		model.addAttribute("result", result);
		model.addAttribute("pageNum", result.getPageNum());
		model.addAttribute("totalPages", result.getPages());
		model.addAttribute("top_key_word",keyword);
		if(keyword!=null&&!"".equals(keyword.trim())&&keyword.length()>1&&keyword.length()<6){
			//去重判断
			
			//插入
			if(false){
				Keywords tmp = new Keywords();
				tmp.setCreateTime(new Date());
				tmp.setKeyWord(keyword);
				tmp.setSearchCount(1);
				keywordsService.add(tmp);
			}else if(pageNum==1){
				//更新搜索次数
				
			}else{
				//翻页的不算
				System.out.println("这是翻译的重复词汇："+keyword);
			}
		}
		return "/index";
	}

	@RequestMapping("/index3")
	public String index3(Model model) {
		System.out.println("success!!!!");
		model.addAttribute("result", "project is running successfully!!!");
		return "/index3";
	}

	@RequestMapping("/index4")
	public String index4(Model model) {
		System.out.println("success!!!!");
		return "/index4";
	}

	/*
	 * 采用spring提供的上传文件的方法
	 */
	@RequestMapping("fileUpload2")
	public String fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request)
			throws IOException {
		long startTime = System.currentTimeMillis();
		System.out.println("fileName：" + file.getOriginalFilename());
		String path = request.getServletContext().getRealPath("") + file.getOriginalFilename();
		System.out.println("==========1111111======" + path);
		File newFile = new File(path);
		// 通过CommonsMultipartFile的方法直接写文件（注意这个时候）
		file.transferTo(newFile);
		String path2 = request.getServletContext().getRealPath("") + System.currentTimeMillis()
				+ file.getOriginalFilename();
		File copyfiled = new File(path2);
		System.out.println("==========3333======" + path2);
		FileUtils.copyFile(newFile, copyfiled);
		long endTime = System.currentTimeMillis();
		System.out.println("方法二的运行时间：" + String.valueOf(endTime - startTime) + "ms");
		return "/success";
	}

	@RequestMapping("/toRegister")
	public String toRegister(Model model) {
		return "user/register";
	}


}